package com.example.cloud.service.inventory.impl;

import com.example.cloud.constants.StateConstants;
import com.example.cloud.dao.inventory.InboundRecordRepository;
import com.example.cloud.dao.inventory.InventoryDetailsRepository;
import com.example.cloud.model.inventory.InboundRecord;
import com.example.cloud.model.inventory.InventoryDetails;
import com.example.cloud.model.system.User;
import com.example.cloud.service.inventory.InventoryDetailsService;
import com.example.cloud.utils.BusinessUtil;
import com.example.cloud.utils.RequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;

/**
 * Created by wangtao on 2018/8/18.
 */
@Service
public class InventoryDetailsServiceImpl implements InventoryDetailsService {

    @PersistenceContext
    EntityManager em;

    @Autowired
    private InventoryDetailsRepository inventoryDetailsRepository;

    @Autowired
    private InboundRecordRepository inboundRecordRepository;

    @Override
    @Transactional
    public void doSave(InventoryDetails record) {
        User user = RequestUtil.getLoginUser();
        // 本次入库数量
        int useInventory = 0;
        int lockInventory = 0;
        int quantity = record.getQuantity();
        if(record.getNormalFlag() == StateConstants.NORMAL_FLAG_NORMAL){
            // 正常品入可用库存
            useInventory = quantity;
        }else if(record.getNormalFlag() == StateConstants.NORMAL_FLAG_ABNORMAL){
            // 异常品入锁定库存
            lockInventory = quantity;
        }
        record.setTotalInventory(quantity);
        record.setUseInventory(useInventory);
        record.setLockInventory(lockInventory);
        BusinessUtil.appendRecord(record);
        inventoryDetailsRepository.save(record);

        // 同步主表库存
        StringBuilder sql = new StringBuilder("update inventory set total_inventory = IFNULL(total_inventory,0) + :quantity");
        sql.append(",use_inventory = IFNULL(use_inventory,0) + :useInventory");
        sql.append(",lock_inventory = IFNULL(lock_inventory,0) + :lockInventory");
        sql.append(" where id = :id");
        Query query = em.createNativeQuery(sql.toString());
        query.setParameter("quantity", quantity);
        query.setParameter("useInventory", useInventory);
        query.setParameter("lockInventory", lockInventory);
        query.setParameter("id", record.getInventoryId());
        query.executeUpdate();

        // 生成入库记录
        InboundRecord ir = new InboundRecord();
        ir.setItemId(record.getItemId());
        ir.setItemName(record.getItemName());
        ir.setTypeId(record.getTypeId());
        ir.setTypeName(record.getTypeName());
        ir.setQuantity(quantity);
        ir.setNormalFlag(record.getNormalFlag());
        ir.setRemark(record.getRemark());
        ir.setAccountId(user.getAccountId());
        ir.setUserId(user.getId());
        ir.setOrganId(user.getOrganId());
        ir.setOrganName(user.getOrganName());
        BusinessUtil.appendRecord(ir);
        inboundRecordRepository.save(ir);
    }
}
